<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String ctx = request.getContextPath();
    request.setAttribute("ctx", ctx);
%>
<html>
<head>
    <title>Title</title>
    <script src="/bootstrap/jquery-1.9.1.min.js"></script>
    <link rel="stylesheet" href="${ctx}/css/index_work.css">
</head>
<body>
<form action="${ctx}/save.do" method="post" enctype="multipart/form-data">
    <input type="hidden" name="did" value="${getid.did}"><br>
    名称:<input type="text" name="dname" value="${getid.dname}"><br>
    日期:<input type="date" name="create_date" value="${getid.create_date}"><br>
    图像:<input type="file" name="file" value="${getid.img}"><br>
    <div id="shops">

    </div><br>
    <input type="button" value="保存" onclick="add()">
</form>
</body>
<script>
    function add() {
        var formData=new FormData($("form")[0]);
        $.ajax({
                 type: "post",
                 url: "${ctx}/save.do",
                 data: formData,
                 processData: false,
                 contentType: false,
                 success: function (res) {
                    if(res){
                        alert("保存成功");
                        location.href="${ctx}/list.do";
                    }else{
                        alert("保存失败");
                    }
                 }
             })
    }
    $.post("${ctx}/getShops.do",function (data) {
        for(let i in data){
            $("#shops").append('<input type="checkbox" name="sids" value="'+data[i].sid+'">'+data[i].sname);
        }
        <c:forEach items="${getid.shops}" var="s">
            $('[name="sids"][value="${s.sid}"]').prop("checked",true);
        </c:forEach>
    })
</script>
</html>
